package yang.learn.controller;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import yang.learn.entity.Contacts;
import yang.learn.entity.User;
import yang.learn.service.ContactsService;
import yang.learn.service.UserService;
import yang.learn.service.impl.ContactsServiceImpl;
import yang.learn.service.impl.UserServiceImpl;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("contacts")
public class ContactsController {

    @Autowired(required = false)
    private ContactsService contactsService;
    @Autowired(required = false)
    private UserService userService;

    @ResponseBody
    @RequestMapping("list")
    public ModelAndView list(User user){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("redirect:/");
        List<Contacts> contactsList = contactsService.queryAll(user);
        List<User> userList = new ArrayList<User>();
        UserService userService = new UserServiceImpl();
        for (Contacts contacts: contactsList){
            userList.add(userService.queryId(new User(contacts.getContacts_id())));
        }
        modelAndView.addObject(userList);
        return modelAndView;
    }

    @ResponseBody
    @RequestMapping("manage")
    public ModelAndView manage(HttpServletRequest request){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("redirect:/");
        HttpSession session = request.getSession();
        session.setAttribute("contacts_id",request.getParameter("contacts_id"));
        return modelAndView;
    }

    @RequestMapping("add")
    public ModelAndView query(@RequestBody User user, HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("add_contacts");
        //查询账户
        User queryAccount = userService.queryAccount(user);
        HttpSession session = request.getSession();
        //获取当前登录的账户id
        int user_id = Integer.parseInt(String.valueOf(session.getAttribute("user_id")));
        Contacts contacts = new Contacts(user_id, queryAccount.getUser_id(),queryAccount.getUser_name());
        if(queryAccount==null){//用户不存在
            contacts.setStats("用户账号不存在！");
            System.out.println("用户账号不存在！");
        }else if(contactsService.query(contacts) != null){//关系已存在
            contacts.setStats("好友关系已存在！");
            System.out.println("好友关系已存在！");
            System.out.println("好友关系:"+contacts);
        }else{//关系不存在
            contactsService.add(contacts);
            System.out.println("好友添加成功！");
        }
        modelAndView.addObject("contacts",contacts);
        return modelAndView;
    }

    @RequestMapping("del")
    public ModelAndView del(@RequestBody User user,HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("del_contacts");
        user = userService.queryAccount(user);
        int user_id = Integer.parseInt(String.valueOf(request.getSession().getAttribute("user_id")));
        contactsService.del(new Contacts(user_id,user.getUser_id()));
        System.out.println("用户ID"+user_id+"删除联系人"+user.getUser_name());
        return modelAndView;
    }

}
